package mark.tom.friend.presenter;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import java.util.concurrent.Callable;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import mark.tom.friend.bean.bo.AppInfoBO;
import mark.tom.friend.contract.AppListContract;
import mark.tom.friend.provider.ISystemProvider;

public class AppListPresenter implements AppListContract.Presenter {

    private AppListContract.View mView;

    @Autowired public ISystemProvider systemProvider;

    @Inject AppListPresenter(AppListContract.View mView) {
        this.mView = mView;
    }

    @Override
    public Observable<AppInfoBO> listAppInfoListByKey(final String key) {
        return Observable
                .defer(new Callable<ObservableSource<PackageInfo>>() {
                    @Override
                    public ObservableSource<PackageInfo> call() {
                        // 获取所有安装的应用信息
                        return Observable.fromIterable(systemProvider.getPackageInfoList());
                    }
                })
                .filter(new Predicate<PackageInfo>() {
                    @Override
                    public boolean test(@NonNull PackageInfo packageInfo) {
                        // 去除系统应用
                        return (ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) == 0;
                    }
                })
                .map(new Function<PackageInfo, AppInfoBO>() {
                    @Override
                    public AppInfoBO apply(@NonNull PackageInfo packageInfo) {
                        // 将应用信息组装成对象
                        AppInfoBO appInfo = new AppInfoBO();
                        appInfo.setAppName(packageInfo.applicationInfo.loadLabel(systemProvider.getPackageManager()).toString());
                        appInfo.setIcon(packageInfo.applicationInfo.loadIcon(systemProvider.getPackageManager()));
                        appInfo.setPackageName(packageInfo.packageName);
                        appInfo.setVersionName(packageInfo.versionName);
                        return appInfo;
                    }
                })
                .filter(new Predicate<AppInfoBO>() {
                    @Override
                    public boolean test(@NonNull AppInfoBO appInfoBO) {
                        // 进行过滤
                        return key == null || appInfoBO.getAppName().contains(key);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public void onStart() {
        mView.refreshListView();
    }

    @Override
    public void onDestroy() {
        mView = null;
    }
}
